package com.example.baselibrary.common;

import android.app.Activity;
import android.app.ActivityManager;

import com.example.baselibrary.R;

import java.util.Stack;

/**
 * Activity管理器 by zqq on 2018/6/9.
 */

public class AppManager {

    private Stack<Activity> activityStack = new Stack<>();

    /*-单例:静态内部类式-*/
    private AppManager(){ }

    private static class Holder {
        private static AppManager appManager = new AppManager();
    }
    public static AppManager getInstance() {
        return Holder.appManager;
    }


    /**
     * Activity入栈
     */
    public void addActivity(Activity activity) {
        activityStack.add(activity);
    }


    /**
     * Activity出栈
     */
    public void removeActivity(Activity activity) {
        if (activityStack != null && activityStack.size() > 0) {
            if (activity != null) {
                activity.finish();
                activityStack.remove(activity);
            }
        }
    }

    /**
     * 获取栈顶的activity，先进后出原则
     */
    public Activity getLastActivity() {
        return activityStack.lastElement();
    }

    /**
     * 获取当前activity
     */
    public Activity getCurrentActivity() {
        if (activityStack.size() > 0) {
            return activityStack.get(activityStack.size() - 1);
        }
        return null;
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            if(!activity.isFinishing()) {
                finish(activity);
            }
        }
    }

    /**
     * 结束指定类名的Activity
     * @param cls 指定的Activity
     */
    public void finishActivity(Class<?> cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
            }
        }
    }

    /**
     * 结束所有activity
     */
    public void finishAllActivity() {
        try {
            for (int i = 0; i < activityStack.size(); i++) {
                if (null != activityStack.get(i)) {
                    activityStack.get(i).finish();
                }
            }
            activityStack.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void finish(Activity activity) {
        if (activity != null) {
            activity.finish();
//            activity.overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);//向右滑动出去
            activity.overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);//向左滑动出去
            activity = null;
        }
    }

    /**
     * 退出应用程序
     */
    public void appExit() {
        try {
            finishAllActivity();
            //退出JVM(java虚拟机),释放所占内存资源,0表示正常退出(非0的都为异常退出)
            System.exit(0);
            //从操作系统中结束掉当前程序的进程
            android.os.Process.killProcess(android.os.Process.myPid());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
